package com.itheima.web;

import com.itheima.pojo.po.Student;
import com.itheima.pojo.vo.PageBean;
import com.itheima.pojo.vo.Pagination;
import com.itheima.pojo.vo.Result;
import com.itheima.service.StudentService;
import com.itheima.util.BaseController;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@WebServlet("/student/*")
public class StudentServlet extends HttpServlet {
    private StudentService service = new StudentService();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置请求和响应编码
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        //设置请求和响应编码
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        //1.获取方法名
        String url = request.getRequestURI();
        int lastIndex = url.lastIndexOf('/');
        String method = url.substring(lastIndex + 1,url.length());


        if(method.equals("selectByPage")) {
            //分页查询功能
            selectByPage(request,response);
        }else if(method.equals("addStu")) {
            //添加数据功能
            addStu(request,response);
        } else if(method.equals("updateStu")) {
            //修改数据功能
            updateStu(request,response);
        } else if(method.equals("deleteStu")) {
            //删除数据功能
            deleteStu(request,response);
        }


    }

    /*
        删除数据功能
     */
    private void deleteStu(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        try{
            //1. 获取请求参数
            String number = req.getParameter("number");


            //2. 调用业务层的删除方法
            service.deleteStu(number);

            // 3 响应
            BaseController.printResult(resp, new Result(true, "删除成功!"));
        } catch (RuntimeException e) {
            e.printStackTrace();
            BaseController.printResult(resp, new Result(false, "删除失败! 原因:" + e.getMessage()));
    }
    }

    /*
        修改数据功能
     */
    private void updateStu(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        try{
            //获取请求参数,封装Student对象
            Student stu = BaseController.parseJSON2Object(req, Student.class);

            //调用业务层的添加方法
            service.updateStu(stu);

            BaseController.printResult(resp,new Result(true,"修改成功!"));
        } catch (RuntimeException e) {
            e.printStackTrace();
            BaseController.printResult(resp, new Result(false, "修改失败!"));
        }
    }

    /*
        添加数据功能
     */
    private void addStu(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        try{
            //获取请求参数,封装Student对象
            Student stu = BaseController.parseJSON2Object(req, Student.class);

            //调用业务层的添加方法
            service.addStu(stu);

            BaseController.printResult(resp,new Result(true,"添加成功!"));
        } catch (RuntimeException e) {
            e.printStackTrace();
            BaseController.printResult(resp, new Result(false, "添加失败!"));
        }
    }

    /*
        分页查询功能
     */
    private void selectByPage(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //获取请求参数
        try{
            Pagination pagination = BaseController.parseJSON2Object(req, Pagination.class);

            //调用业务层的查询方法
            PageBean page = service.selectByPage(pagination);
//            int i = 1/0;
            //响应给客户端
            BaseController.printResult(resp,new Result(true,"查询成功!",page));
        } catch (RuntimeException e) {
            e.printStackTrace();
            BaseController.printResult(resp, new Result(false, "查询失败!"));
        }
    }



}
